package com.pig4cloud.pig.building.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.building.entity.PropertyBuilding;
import com.pig4cloud.pig.building.service.BuildingService;
import com.pig4cloud.pig.common.core.util.R;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/building" )
@Tag(description = "building" , name = "楼宇管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BuildingController {

	private final BuildingService buildingService;
//查询
    @GetMapping("/page")
	public R getBuildingPage(@ParameterObject Page page, @ParameterObject PropertyBuilding propertyBuilding) {
		return R.ok(buildingService.selectAll(page,propertyBuilding));
	}

	@GetMapping("/getAll")
	public R getAll(){
		return R.ok(buildingService.list());
	}

//回显
	@GetMapping("/getBuilding")
	public R getBuilding(@RequestParam Long buildingId) {
		PropertyBuilding building = buildingService.getById(buildingId);
		return R.ok(building);
	}
//添加校验
	@PostMapping("/addBuilding")
	public R addBuilding(@RequestBody PropertyBuilding buildingEntity) {
		System.out.println(buildingEntity);
		PropertyBuilding propertyBuilding = buildingService.checkByBuildingName(buildingEntity);
		if (propertyBuilding != null) {
			return R.failed("楼宇已存在");
		}
		return R.ok(buildingService.save(buildingEntity));
	}
//更新校验
	@PutMapping("/updateBuilding")
	public R updateBuilding(@RequestBody PropertyBuilding buildingEntity) {
		PropertyBuilding propertyBuilding = buildingService.checkByBuildingName(buildingEntity);
		if (propertyBuilding != null) {
			return R.failed("楼宇已存在");
		}
		return R.ok(buildingService.updateById(buildingEntity));
	}
//删除
	@DeleteMapping("/deleteBuilding")
	public R deleteBuilding(@RequestParam Long[] ids) {
		buildingService.removeBatchByIds(CollUtil.toList(ids));
		return R.ok(null, "删除成功");
	}
}
